//
//  File.swift
//  elarc01
//
//  Created by elarc on 16/4/13.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation
//import LiquidFloatingActionButton

class NewIdealViewController: AddInShoppingCarAnimation, LiquidFloatingActionButtonDataSource, LiquidFloatingActionButtonDelegate{
    
//    CircleProgressView
    var circleProgress:CircleProgressView?
    
    //tabbar 转场
    lazy var gesture: UIPanGestureRecognizer = {
        let gesture = UIPanGestureRecognizer(target: self, action: #selector(NewIdealViewController.swipeTransition(_:)))
        return gesture
    }()

    func swipeTransition(sender: UIPanGestureRecognizer) {
        switch sender.state {
        case .Began :
            print("Began")
            if sender.translationInView(sender.view).x >= 0 {
                tabBarController?.tr_selected(1, gesture: sender)
            }
        default : break
        }
    }
    
    var cells: [LiquidFloatingCell] = []
    var floatingActionButton: LiquidFloatingActionButton!
    
    var tempImage:UIImageView?
    var tempBtn:UIButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addGestureRecognizer(gesture)
        self.view.backgroundColor = UIColor.whiteColor()
        tempImage = UIImageView()
        tempImage?.image = UIImage(named: "tabbar_home")
        tempImage!.frame = CGRectMake(100, 100, 100, 49)
        view.addSubview(tempImage!)
      
        
        tempBtn = UIButton(type: UIButtonType.Custom)
        tempBtn?.titleLabel?.text = "click我"
        tempBtn?.backgroundColor = UIColor.blackColor()
        tempBtn?.frame = CGRectMake(200, 400, 100, 50)
        view?.addSubview(tempBtn!)
        tempBtn?.addTarget(self, action: #selector(NewIdealViewController.pushTo), forControlEvents: UIControlEvents.TouchUpInside)
        
//      liquit
        buildLiquit()
//      circleProgress
        builCircleProgress()
    }
    
    func buildLiquit() -> Void {
        //liquit
        let createButton: (CGRect, LiquidFloatingActionButtonAnimateStyle) -> LiquidFloatingActionButton = { (frame, style) in
            let floatingActionButton = LiquidFloatingActionButton(frame: frame)
            floatingActionButton.animateStyle = style
            floatingActionButton.dataSource = self
            floatingActionButton.delegate = self
            return floatingActionButton
        }
        
        let cellFactory: (String) -> LiquidFloatingCell = { (iconName) in
            return LiquidFloatingCell(icon: UIImage(named: iconName)!)
        }
        cells.append(cellFactory("ic_cloud"))
        cells.append(cellFactory("ic_system"))
        cells.append(cellFactory("ic_place"))
        
        let floatingFrame = CGRect(x: self.view.frame.width - 56 - 16, y: self.view.frame.height - 56 - 16 - 64, width: 56, height: 56)
        let bottomRightButton = createButton(floatingFrame, .Up)
        
        let floatingFrame2 = CGRect(x: 16, y: 16 + 64, width: 56, height: 56)
        let topLeftButton = createButton(floatingFrame2, .Down)
        
        self.view.addSubview(bottomRightButton)
        self.view.addSubview(topLeftButton)
    }
    
    func builCircleProgress() -> Void {
        circleProgress = CircleProgressView()
        circleProgress?.frame = CGRectMake(100, 200, ScreenWidth-200, ScreenWidth-200)
        circleProgress?.backgroundColor = UIColor.redColor()
        circleProgress?.progress = 0.2
        
        self.view.addSubview(circleProgress!)
        
    }
    
    func pushTo(){
        self.addProductsAnimation(tempImage!)
        self.addProductsToBigShopCarAnimation(tempImage!)
        tempImage!.boom()
    }
    
    func numberOfCells(liquidFloatingActionButton: LiquidFloatingActionButton) -> Int {
        return cells.count
    }
    
    func cellForIndex(index: Int) -> LiquidFloatingCell {
        return cells[index]
    }
    
    func liquidFloatingActionButton(liquidFloatingActionButton: LiquidFloatingActionButton, didSelectItemAtIndex index: Int) {
        print("did Tapped! \(index)")
        liquidFloatingActionButton.close()
    }
    
    
    //放大镜效果
    var magnifier:Magnifier?
    var touchTimer:NSTimer?
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        self.touchTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector:#selector(NewIdealViewController.addloop), userInfo: nil, repeats: false)
        
        if (self.magnifier == nil) {
            self.magnifier = Magnifier()
            self.magnifier!.viewToMagnify = self.view;
        }
        for touch: AnyObject in touches {
            let touch:UITouch = touch as! UITouch
            self.magnifier?.touchPoint = touch.locationInView(self.view)
            self.magnifier?.setNeedsDisplay()
        }
    }
    
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        handleAction(touches)
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.touchTimer?.invalidate()
        self.touchTimer = nil
        self.magnifier = nil
    }
    
    func handleAction(timerObj:AnyObject) -> Void {
        for touch: AnyObject in timerObj as! NSSet {
            let touch:UITouch = touch as! UITouch
            self.magnifier?.touchPoint = touch.locationInView(self.view)
            self.magnifier?.setNeedsDisplay()
        }
    }
    
    func addloop() -> Void {
        self.magnifier?.makeKeyAndVisible()
    }
}

